﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:DepartMent
    /// </summary>
    public partial class DepartMent
    {
        public DepartMent()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("DeptFID", "DepartMent");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int DeptFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from DepartMent");
            strSql.Append(" where DeptFID=@DeptFID");
            var parameters = new SqlParameter[] { new SqlParameter("@DeptFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = DeptFID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.DepartMent model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into DepartMent(");
            strSql.Append("DtypeID,Parid,Leveal,DSonnum,DSoncount,Dusercode,Dfullname,DName,DComment,DNamePY)");
            strSql.Append(" values (");
            strSql.Append("@DtypeID,@Parid,@Leveal,@DSonnum,@DSoncount,@Dusercode,@Dfullname,@DName,@DComment,@DNamePY)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@DtypeID", SqlDbType.VarChar, 25),
                    new SqlParameter("@Parid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@DSonnum", SqlDbType.Int, 4),
                    new SqlParameter("@DSoncount", SqlDbType.Int, 4),
                    new SqlParameter("@Dusercode", SqlDbType.VarChar, 100),
                    new SqlParameter("@Dfullname", SqlDbType.VarChar, 200),
                    new SqlParameter("@DName", SqlDbType.VarChar, 100),
                    new SqlParameter("@DComment", SqlDbType.VarChar, 256),
                    new SqlParameter("@DNamePY", SqlDbType.VarChar, 1000) };
            parameters[0].Value = model.DtypeID;
            parameters[1].Value = model.Parid;
            parameters[2].Value = model.Leveal;
            parameters[3].Value = model.DSonnum;
            parameters[4].Value = model.DSoncount;
            parameters[5].Value = model.Dusercode;
            parameters[6].Value = model.Dfullname;
            parameters[7].Value = model.DName;
            parameters[8].Value = model.DComment;
            parameters[9].Value = model.DNamePY;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.DepartMent model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update DepartMent set ");
            strSql.Append("DtypeID=@DtypeID,");
            strSql.Append("Parid=@Parid,");
            strSql.Append("Leveal=@Leveal,");
            strSql.Append("DSonnum=@DSonnum,");
            strSql.Append("DSoncount=@DSoncount,");
            strSql.Append("Dusercode=@Dusercode,");
            strSql.Append("Dfullname=@Dfullname,");
            strSql.Append("DName=@DName,");
            strSql.Append("DComment=@DComment,");
            strSql.Append("DNamePY=@DNamePY");
            strSql.Append(" where DeptFID=@DeptFID");
            var parameters = new SqlParameter[] { new SqlParameter("@DtypeID", SqlDbType.VarChar, 25),
                    new SqlParameter("@Parid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@DSonnum", SqlDbType.Int, 4),
                    new SqlParameter("@DSoncount", SqlDbType.Int, 4),
                    new SqlParameter("@Dusercode", SqlDbType.VarChar, 100),
                    new SqlParameter("@Dfullname", SqlDbType.VarChar, 200),
                    new SqlParameter("@DName", SqlDbType.VarChar, 100),
                    new SqlParameter("@DComment", SqlDbType.VarChar, 256),
                    new SqlParameter("@DNamePY", SqlDbType.VarChar, 1000),
                    new SqlParameter("@DeptFID", SqlDbType.Int, 4) };
            parameters[0].Value = model.DtypeID;
            parameters[1].Value = model.Parid;
            parameters[2].Value = model.Leveal;
            parameters[3].Value = model.DSonnum;
            parameters[4].Value = model.DSoncount;
            parameters[5].Value = model.Dusercode;
            parameters[6].Value = model.Dfullname;
            parameters[7].Value = model.DName;
            parameters[8].Value = model.DComment;
            parameters[9].Value = model.DNamePY;
            parameters[10].Value = model.DeptFID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int DeptFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from DepartMent ");
            strSql.Append(" where DeptFID=@DeptFID");
            var parameters = new SqlParameter[] { new SqlParameter("@DeptFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = DeptFID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string DeptFIDlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from DepartMent ");
            strSql.Append(" where DeptFID in (" + DeptFIDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.DepartMent GetModel(int DeptFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 DeptFID,DtypeID,Parid,Leveal,DSonnum,DSoncount,Dusercode,Dfullname,DName,DComment,DNamePY from DepartMent ");
            strSql.Append(" where DeptFID=@DeptFID");
            var parameters = new SqlParameter[] { new SqlParameter("@DeptFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = DeptFID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.DepartMent DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.DepartMent();
            if (row != null)
            {
                if (row["DeptFID"] != null && row["DeptFID"].ToString() != string.Empty)
                {
                    model.DeptFID = int.Parse(row["DeptFID"].ToString());
                }
                if (row["DtypeID"] != null)
                {
                    model.DtypeID = row["DtypeID"].ToString();
                }
                if (row["Parid"] != null)
                {
                    model.Parid = row["Parid"].ToString();
                }
                if (row["Leveal"] != null && row["Leveal"].ToString() != string.Empty)
                {
                    model.Leveal = int.Parse(row["Leveal"].ToString());
                }
                if (row["DSonnum"] != null && row["DSonnum"].ToString() != string.Empty)
                {
                    model.DSonnum = int.Parse(row["DSonnum"].ToString());
                }
                if (row["DSoncount"] != null && row["DSoncount"].ToString() != string.Empty)
                {
                    model.DSoncount = int.Parse(row["DSoncount"].ToString());
                }
                if (row["Dusercode"] != null)
                {
                    model.Dusercode = row["Dusercode"].ToString();
                }
                if (row["Dfullname"] != null)
                {
                    model.Dfullname = row["Dfullname"].ToString();
                }
                if (row["DName"] != null)
                {
                    model.DName = row["DName"].ToString();
                }
                if (row["DComment"] != null)
                {
                    model.DComment = row["DComment"].ToString();
                }
                if (row["DNamePY"] != null)
                {
                    model.DNamePY = row["DNamePY"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select DeptFID,DtypeID,Parid,Leveal,DSonnum,DSoncount,Dusercode,Dfullname,DName,DComment,DNamePY ");
            strSql.Append(" FROM DepartMent ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" DeptFID,DtypeID,Parid,Leveal,DSonnum,DSoncount,Dusercode,Dfullname,DName,DComment,DNamePY ");
            strSql.Append(" FROM DepartMent ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM DepartMent ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.DeptFID desc");
            }
            strSql.Append(")AS Row, T.*  from DepartMent T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录行
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public DataRow GetRecordByCode(string Code)
        {
            return null;
        }
    }
}

